import SDKManager from "../SDKManager";
import zijieConfig from "../zijieConfig";





class RewardVideoObject {
    public _rewardvideo_name: string = "";
    public _rewardvideo_num: number = 0;
    public _rewardvideo_open: boolean = false;
}

export default class ksRewardVideoManager {

    public static _s_instance: ksRewardVideoManager | null = null;
    public static getInstance(): ksRewardVideoManager {
        if (!this._s_instance) {
            this._s_instance = new ksRewardVideoManager();
            this._s_instance.initRewardVideo();
        }

        return this._s_instance;
    }

    tt: any = null;
    private _rewardvideoAd: any = null;
    private _isLoadRewardVideoAd: boolean = false;

    private initRewardVideo() {
        this.tt = window["ks"] || undefined;

        if (this.tt == null)
            return;
        if (this.tt == undefined)
            return;

        this._rewardvideoAd = this.tt.createRewardedVideoAd({
            adUnitId: zijieConfig._ad_rewardvideo,
        });

        this.loadRewardVideoAd();

        this.addRewardVideoEvent();
    }

    showLoading(): void {
        this.tt = window["ks"] || undefined;
        if (this.tt == null)
            return;
        if (this.tt == undefined)
            return;
        this.tt.showLoading({
            title: "请求中，请稍后...",
            success(res) {
                console.log(`${res}`);
            },
            fail(res) {
                console.log(`showLoading调用失败`);
            },
        });
    }

    hideLoading(): void {
        this.tt = window["ks"] || undefined;
        if (this.tt == null)
            return;
        if (this.tt == undefined)
            return;
        this.tt.hideLoading()
    }

    /**
     * 激励广告事件监听
     * @returns 
     */
    private addRewardVideoEvent() {
        if (this.tt == undefined)
            return;
        this._rewardvideoAd.onClose(({ isEnded }) => {
            if (isEnded) {
                // 给予奖励 
                SDKManager.onRewardVideoCloseComplete();
                console.log("rewardvideo完成观看");

                SDKManager.getInstance().whenAdClick();
            }
            else {
                SDKManager.onRewardVideoClose();
                console.log("rewardvideo未完成观看");
            }
            // this.hideLoading();
            this._isLoadRewardVideoAd = false;
            this.loadRewardVideoAd();
        });
        this._rewardvideoAd.onError(err => {
            console.error("rewardvideo广告加载出现问题", err, "==", zijieConfig._ad_rewardvideo);
        })
    }

    /**
     * 加载激励事件
     * @returns 
     */
    private loadRewardVideoAd() {
        if (this.tt == undefined)
            return;

        this._isLoadRewardVideoAd = true;
    }

    /**
     * 展示激励
     * @returns 
     */
    public showRewardVideoAd() {
        if (this.tt == undefined)
            return;
        console.log("点击了激励视频2")
        if (this._isLoadRewardVideoAd) {
            console.log("点击了激励视频3")
            // this.showLoading();

            this._rewardvideoAd
                .show()
                .catch((err) => {
                    console.error("rewardvideo广告展示出现问题", err);
                    SDKManager.onRewardVideoClose();
                    this.loadRewardVideoAd();
                });
        }
        else
            this.loadRewardVideoAd();
    }
    /**
     * 展示广告时，没有广告，请求一次
     */
    public addRewardVideoAd() {
        this.loadRewardVideoAd();
    }


    /**
     * 点击解锁
     * @param pNode 点击的node
     * @param callbackTarget 回调的node
     * @param callback 回调的方法
     */
    public onClickLock(pNode:  cc.Node, callbackTarget: any = null, callback: Function = function () { }, callback2: Function = function () { }) {
        this.openLock = pNode;
        this._closeCallbackTarget = callbackTarget;
        this._closeCallback = callback;
        this._closeCallback2 = callback2;

        console.log("点击了激励视频1")
        this.showRewardVideoAd();
    }

    /**
     * 激励视频关闭
     * @param pBool true-解锁 false-取消
     */
    public onRewardVideoClose(pBool: boolean) {
        SDKManager.getInstance().reward_node=null;
        if (!pBool) {
            console.log("激励广告未完成")
            if (this._closeCallback2)
                this._closeCallback2();
            return;
        }


        if (this._closeCallback)
            this._closeCallback();
    }

    private _openLock:  cc.Node | null = null;
    /**
     * 带锁的道具，被点击时记录下来
     */
    public set openLock(pNode:  cc.Node | null) {
        this._openLock = pNode;
    }
    public get openLock():  cc.Node | null {
        return this._openLock;
    }

    private _rewardVideoSuccess: boolean = false;
    /**
     * 记录激励广告是否请求成功
     */
    public set rewardVideoSuccess(pBool: boolean) {
        this._rewardVideoSuccess = pBool;
    }
    public get rewardVideoSuccess(): boolean {
        return this._rewardVideoSuccess;
    }

    private _closeCallbackTarget: any = null;
    private _closeCallback: Function = null;
    private _closeCallback2: Function = null;
}
